﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Yeex.Boss.Web.Helper
{
    public static class ValidateCodeHelper
    {
        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">名称</param>
        /// <param name="length">长度</param>
        /// <param name="htmlAttributes">Html属性</param>
        /// <returns></returns>
        public static MvcHtmlString ValidateImage(this HtmlHelper htmlHelper, string name, int? length, IDictionary<string, object> htmlAttributes)
        {
            UrlHelper url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            string src = url.Action("validate", "account", new { id = length, d = Math.Round(5d) });
            string clickEvent = string.Format("reloadVCode(this{0});", length.HasValue ? string.Format(",{0}", length.Value.ToString()) : string.Empty);
            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            string alt = "点击更换图片";
            TagBuilder tagBuilder = new TagBuilder("img");
            tagBuilder.MergeAttribute("alt", alt);
            tagBuilder.MergeAttribute("onclick", clickEvent);
            tagBuilder.MergeAttributes<string, object>(htmlAttributes);
            tagBuilder.MergeAttribute("name", fullHtmlFieldName, true);
            tagBuilder.MergeAttribute("src", src, true);

            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">名称</param>
        /// <param name="length">长度</param>
        /// <param name="htmlAttributes">Html属性</param>
        /// <returns></returns>
        public static MvcHtmlString ValidateImage(this HtmlHelper htmlHelper, string name, int? length, object htmlAttributes)
        {
            return htmlHelper.ValidateImage(name, length, htmlAttributes as IDictionary<string, object>);
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">名称</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static MvcHtmlString ValidateImage(this HtmlHelper htmlHelper, string name, int? length)
        {
            return htmlHelper.ValidateImage(name, length, null);
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">名称</param>
        /// <param name="htmlAttributes">Html属性</param>
        /// <returns></returns>
        public static MvcHtmlString ValidateImage(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes)
        {
            return htmlHelper.ValidateImage(name, null, htmlAttributes);
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">名称</param>
        /// <param name="htmlAttributes">Html属性</param>
        /// <returns></returns>
        public static MvcHtmlString ValidateImage(this HtmlHelper htmlHelper, string name, object htmlAttributes)
        {
            return htmlHelper.ValidateImage(name, null, htmlAttributes as IDictionary<string, object>);
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static MvcHtmlString ValidateImage(this HtmlHelper htmlHelper, string name)
        {
            return htmlHelper.ValidateImage(name, null, null);
        }
    }
}